package com.arancia.codinglife.rocketmq.client;

import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.StopWatch;

import javax.annotation.PostConstruct;

@Component
public class RocketMQClient {
//	@Value("${apache.rocketmq.producer.producerGroup}")
	private String producerGroup = "Producer";

//	@Value("${apache.rocketmq.namesrvAddr}")
	private String namesrvAddr = "localhost:9876";

	@PostConstruct
	public void defaultMQProducer() {
		DefaultMQProducer producer = new DefaultMQProducer(producerGroup);
		producer.setNamesrvAddr(namesrvAddr);

		try {
			producer.start();
			Message message = new Message("SELF_TEST_TOPIC", "push", "发送消息---aranciacheng---".getBytes(RemotingHelper.DEFAULT_CHARSET));

			StopWatch stop = new StopWatch();
			stop.start();

			for (int i = 0; i < 1000; i++) {
				SendResult result = producer.send(message);
				System.out.println("发送响应: MsgId:" + result.getMsgId() + "，发送状态：" + result.getSendStatus());
			}
			stop.stop();
			System.out.println("--------------------发送1000条消息耗时:" + stop.getTotalTimeMillis());
		} catch (Exception e) {
			System.out.println("sayhi");
			e.printStackTrace();
		} finally {
			producer.shutdown();
		}
	}
}
